package com.imist.mybatisdemo2.servlet;

import com.imist.mybatisdemo2.dao.UserDao;
import com.imist.mybatisdemo2.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/delete")
public class UserDelServlet extends HttpServlet {
    private UserDao userDao = new UserDao();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String id = req.getParameter("id");
        String type = req.getParameter("type");

        //执行删除或者锁定
        if ("lock".equals(type)){
            //锁定操作 update操作
            User user = new User();
            user.setId(Integer.parseInt(id));
            user.setUserStatus(1);
            //因为这里是动态的sql只需要传入id和要修改的字段即可
            userDao.updateUser(user);
        } else if ("unlock".equals(type)){
            //解锁操作
            User user = new User();
            user.setId(Integer.parseInt(id));
            user.setUserStatus(0);
            userDao.updateUser(user);
        }else if ("delete".equals(type)){
            userDao.delUser(Integer.parseInt(id));
        }else {
            System.out.println("参数错误...");
        }
        //跳转到首页
        resp.sendRedirect("/findUsers");
    }
}
